package com.ruicar.afs.cloud.basic.proceeds.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicExpenseRecord;
import com.ruicar.afs.cloud.basic.common.entity.BasicRepaymentPlan;
import com.ruicar.afs.cloud.basic.common.service.BasicExpenseRecordService;
import com.ruicar.afs.cloud.basic.common.service.BasicRepaymentPlanService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ExpenseStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.RepaymentStatusEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 合同费用controller
 */
@Slf4j
@RestController
@RequestMapping("/expenseInfo")
@AllArgsConstructor
public class ExpenseController {

    private BasicExpenseRecordService basicExpenseRecordService;
    private BasicRepaymentPlanService basicRepaymentPlanService;

    @ApiOperation(value = "根据合同号查询差额信息")
    @PostMapping("/findByContractNoExpenseInfo")
    private IResponse findByContractNoExpenseInfo(@RequestParam("contractNo") String contractNo){
        List<BasicExpenseRecord> basicExpenseRecords = basicExpenseRecordService.list(Wrappers.<BasicExpenseRecord>query().lambda()
                .eq(BasicExpenseRecord::getContractNo, contractNo)
                .eq(BasicExpenseRecord::getStatus, ExpenseStatusEnum.unsettle));

        List<BasicRepaymentPlan> basicRepaymentPlan = basicRepaymentPlanService.list(Wrappers.<BasicRepaymentPlan>query().lambda()
                .eq(BasicRepaymentPlan::getContractNo, contractNo)
                .or().eq(BasicRepaymentPlan::getStatus, RepaymentStatusEnum.UNPAID)
                .eq(BasicRepaymentPlan::getStatus, RepaymentStatusEnum.OVERDUE));

        Map map = new HashMap();
        map.put("basicExpenseRecords", basicExpenseRecords);
        map.put("basicRepaymentPlan", basicRepaymentPlan);
        return new IResponse().setData(map);
    }
}
